package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.QueryWrapperUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import com.sk.bilibili.bean.ThemBanner;
import com.sk.bilibili.mapper.ThemBannerMapper;

/**
 * @author 邵康
 * @date 2021/11/27 10:21
 */
@Service
public class ThemBannerService extends ServiceImpl<ThemBannerMapper, ThemBanner> {

    @Autowired
    private MinioService minioService;
    @Autowired
    private ThemService themService;
    private static final String COVER_PATH = "themBanner/cover/%s.jpg";
    private static final Pattern COVER_PATTERN = Pattern.compile("/(themBanner/cover/.*jpg)");
    private static final String SMALLCOVER_PATH = "themBanner/smallCover/%s.jpg";
    private static final Pattern SMALLCOVER_PATTERN = Pattern.compile("/(themBanner/smallCover/.*jpg)");

    public R<Void> add(ThemBanner themBanner) {
        themBanner.setCover(minioService.add(themBanner.getCoverFile(), COVER_PATH));
        themBanner.setSmallCover(minioService.add(themBanner.getSmallCoverFile(), SMALLCOVER_PATH));
        save(themBanner);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        ThemBanner temp = getById(id);
        minioService.delete(temp.getCover(), COVER_PATTERN);
        minioService.delete(temp.getSmallCover(), SMALLCOVER_PATTERN);
        removeById(id);
        return R.success();
    }

    public R<Void> update(ThemBanner themBanner) {
        ThemBanner temp = getById(themBanner.getId());
        String cover = minioService.update(COVER_PATH, COVER_PATTERN, temp.getCover(), themBanner.getCoverFile());
        themBanner.setCover(cover);
        String smallCover = minioService.update(SMALLCOVER_PATH, SMALLCOVER_PATTERN, temp.getSmallCover(), themBanner.getSmallCoverFile());
        themBanner.setSmallCover(smallCover);
        updateById(themBanner);
        return R.success();
    }

    public R<IPage<ThemBanner>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page, size), null));
    }

    public R<List<ThemBanner>> getThemBanner(Integer typeId) {
        QueryWrapper<ThemBanner> queryWrapper = QueryWrapperUtils.eq("type_id", typeId);
        return R.success(list(queryWrapper));
    }

    // 根据 追番量 推荐
    public void updateThemBanner() {
        List<ThemBanner> themBanners = list(null);
        QueryWrapper<Them> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("follow_num");
        queryWrapper.last("limit "+themBanners.size());
        List<Them> thems = themService.list(queryWrapper);
        for (int i = 0; i < themBanners.size(); i++) {
            ThemBanner themBanner = themBanners.get(i);
            themBanner.setUrl(String.format("play/%d",thems.get(i).getId()));
            updateById(themBanner);
        }
    }

}
